package com.imti.fragmentdemo;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

public class MyViewModel extends ViewModel
{

    private MutableLiveData<String> data = new MutableLiveData<>();

    private static MyViewModel sInstance;

    private MyViewModel()
    {
    }

    public static MyViewModel getInstance()
    {
        if (null == sInstance)
        {
            sInstance = new MyViewModel();
        }

        return sInstance;
    }

    //赋值
    public void setData(String str)
    {
        data.postValue(str);
    }
    //取值
    public void getStr(LifecycleOwner lo, Observer<String> observer)
    {
        data.observe(lo, observer);
    }
    //移除监听
    public void removeObserver(Observer<String> observer)
    {
        data.removeObserver(observer);
    }
}
